<!DOCTYPE html>
<html>

<head>
  <meta charset="utf-8">
  <meta name="renderer" content="webkit|ie-comp|ie-stand">
  <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
  <meta name="viewport" content="width=device-width,initial-scale=1,minimum-scale=1.0,maximum-scale=1.0,user-scalable=no" />
  <meta http-equiv="Cache-Control" content="no-siteapp" />
  <title>采购单入库</title>
</head>

<body>
  <div id="app" v-loading.fullscreen.lock="loading">
    <div class="purchaseWarehouseIn">
      <div>
        <el-button type="primary" @click="isShowForm = true">采购入库查询</el-button>
        <el-button type="primary" @click="saveWarehouseIn">确认入库</el-button>
      </div>
      <div style="margin-top:10px">
        <el-table fit stripe border :data="purchaseOrderData" style="width:100%;" @select-all="checkOrder" @select="checkOrder">
          <el-table-column :resizable="false" prop="purchaseType" label="采购类型" :auto-width="true" min-width="70px"></el-table-column>
          <el-table-column :resizable="false" prop="purchaseId" label="采购单号" :auto-width="true" min-width="70px"></el-table-column>
          <el-table-column :resizable="false" prop="materialName" label="物料名称" :auto-width="true" min-width="70px"></el-table-column>
          <el-table-column :resizable="false" prop="color" label="编号" :auto-width="true" min-width="60px"></el-table-column>
          <el-table-column :resizable="false" prop="colorDescription" label="描述" :auto-width="true" min-width="70px"></el-table-column>
          <el-table-column :resizable="false" prop="model" label="规格" ::auto-width="true" min-width="60px"></el-table-column>
          <el-table-column :resizable="false" prop="unit" label="采购单位" width="100px"></el-table-column>
          <el-table-column :resizable="false" prop="unitPrice" label="采购单价" width="100px"></el-table-column>
          <el-table-column :resizable="false" prop="purchaseCount" label="采购数量" width="100px"></el-table-column>
          <el-table-column :resizable="false" label="入库单位" width="160px">
            <template slot-scope="props">
              <el-select v-model="props.row.inCountUnit" style="width:152px" clearable placeholder="请选择">
                <el-option v-for="item in unitList" :key="item" :label="item" :value="item">
                </el-option>
              </el-select>
            </template>
          </el-table-column>
          <el-table-column :resizable="false" label="入库单价" width="200px">
            <template slot-scope="props">
              <el-input-number v-model="props.row.inCountPrice" style="width:192px" controls-position="right" :precision="2"
               :min="0"></el-input-number>
            </template>
          </el-table-column>
          <el-table-column v-if="isRowShow()" :resizable="false" prop="factoryNo" label="工厂型号" :auto-width="true" min-width="70px"></el-table-column>
          <el-table-column v-if="isRowShow()" :resizable="false" prop="coustomId" label="客户型号" :auto-width="true" min-width="70px"></el-table-column>
          <el-table-column v-if="isRowShow()" :resizable="false" prop="orderId" label="订单编号" :auto-width="true" min-width="70px"></el-table-column>
          <el-table-column :resizable="false" label="采购入库数量" width="200px">
            <template slot-scope="props">
              <el-input-number v-model="props.row.count" style="width:192px" controls-position="right" :min="0" :precision="2"
               :max="props.row.oldPurchaseCount"></el-input-number>
            </template>
          </el-table-column>
          <el-table-column :resizable="false" label="入库结算数量" width="200px">
            <template slot-scope="props">
              <el-input-number v-model="props.row.sumMoneyCount" style="width:192px" controls-position="right" :min="0"
               :precision="2"></el-input-number>
            </template>
          </el-table-column>
          <el-table-column :resizable="false" label="仓位" width="180px">
            <template slot-scope="props">
              <el-input v-model="props.row.positions" style="width:170px"></el-input>
            </template>
          </el-table-column>
          <el-table-column :resizable="false" type="selection" min-width="50px"></el-table-column>
        </el-table>
      </div>
      <div style="margin-top:10px;text-align:right;" v-if="pagination.totalCount!==0">
        <el-pagination background :page-size="pagination.pageSize" :total="pagination.totalCount" :current-page.sync="pagination.currentPage"
         @current-change="pageChange"></el-pagination>
      </div>
    </div>

    <el-dialog title="采购入库条件" :visible.sync="isShowForm" width="100%" top="0px" :show-close="false">
      <warehouse-form :search="search" :approval-users="approvalUsers" :warehouses="warehouses" @search-data="searchData"></warehouse-form>
    </el-dialog>
  </div>
  <script src="../../myJs/importFile.js"></script>
  <script>
    importFile();
  </script>
  <script src="warehouseForm.js"></script>
  <script type="text/javascript">
    var app = new Vue({
      el: '#app',
      data: function () {
        // 搜索条件
        var search = {
          purchaseOrder: '',
          orderId: '',
          approvalUser: '',
          warehouse: ''
        };
        return {
          loading: false,
          moment: moment,
          isShowForm: false,
          purchaseOrderData: [],
          unitList: [],
          approvalUsers: [],
          warehouses: [{
              id: 1,
              name: "原材料仓"
            },
            {
              id: 2,
              name: "报废仓"
            },
            {
              id: 3,
              name: "待处理仓"
            }
          ],
          checkAll: [],
          search: _.cloneDeep(search),
          oldSearch: _.cloneDeep(search),
          pagination: {
            currentPage: 1,
            totalCount: 0,
            pageSize: 10
          }
        };
      },
      created: function () {
        this.setApprovalUsers();
        this.setUnitList();
      },
      methods: {
        searchData: function () {
          this.isShowForm = false;
          this.setPurchaseWarehouseInData(true);
        },
        pageChange: function () {
          this.setPurchaseWarehouseInData();
        },
        //设置单位
        setUnitList: function () {
          var self = this;
          ajaxRequest("basicData/getCompanyUnit").then(function (res) {
            self.unitList = res;
          }).catch(function () {
          });
        },
        //查询验收人
        setApprovalUsers: function () {
          var self = this;
          ajaxRequest("user/getDepartmentUserOnValid", {
            "deptName": "仓库"
          }).then(function (res) {
            self.approvalUsers = res;
          }).catch(function () {
          });
        },
        //查询数据
        setPurchaseWarehouseInData: function (isSearch) {
          if (!isSearch) {
            this.search = _.cloneDeep(this.oldSearch);
          }
          var search = this.search;
          var postData = {}
          if (search.orderId.trim() === '' && search.purchaseOrder.trim() === '') {
            this.$alert('订单号与采购单号至少填一个', '消息');
            return;
          }
          postData.orderId = search.orderId;
          postData.purchaseOrder = search.purchaseOrder;
          //开启loading 
          this.loading = true;
          var self = this;
          //请求数据  
          ajaxRequest("companyMaterial/getPurchaseMaterialDate", postData, false).then(function (res) {
            var data = res.returnData;
            if (isSearch) {
              self.oldSearch = _.cloneDeep(self.search);
            }
            var unitList = self.unitList;
            for (var i = 0; i < data.length; i++) {
              for (var j = 0; i < unitList.length; j++) {
                if (data[i].unit === unitList[j]) {
                  data[i].inCountUnit = unitList[j];
                  break;
                };
              }
              data[i].oldPurchaseCount = data[i].count;
              data[i].positions = '';
              if (data[i].type === 1) {
                data[i].purchaseType = '订单采购';
              } else {
                data[i].purchaseType = '库存采购';
              }

            }
            self.purchaseOrderData = data;
            self.loading = false;
          }).catch(function () {
            self.loading = false;
          });
        },
        //保存入库
        saveWarehouseIn: function () {
          this.oldSearch = _.cloneDeep(this.search);
          var search = this.oldSearch;
          var requestUrl = "companyMaterial/savdInWarehouse";
          var warehouseAll = this.checkAll;
          var jsonArr = [];
          if (search.approvalUser === '') {
            this.$alert('请选择验收人', '信息');
            return;
          }
          if (search.warehouse === '') {
            this.$alert('请选择仓库', '信息');
            return;
          }
          if (warehouseAll.length <= 0) {
            this.$alert('请勾选入库行', '信息');
            return;
          }
          for (var i = 0; i < warehouseAll.length; i++) {
            var data = warehouseAll[i];
            var postData = {};
            postData.approvalUser = search.approvalUser;
            if (search.warehouse !== 1) {
              postData.defaultWarehouse = search.warehouse;
              requestUrl = "companyMaterial/saveInOtherWarehouse";
            }
            if (data.count === 0) {
              this.$alert('采购入库数量不能为0', '消息');
              return;
            }
            if (data.sumMoneyCount === 0) {
              this.$alert('入库结算数量不能为0', '消息');
              return;
            }
            if (data.inCountUnit === '') {
              this.$alert('入库单位不能为空', '消息');
              return;
            }
            postData.purchaseId = data.purchaseId;
            postData.type = data.type;
            postData.purchaseDetailId = data.purchaseDetailId;
            postData.materialCode = data.materialCode;
            postData.count = data.count;
            postData.positions = data.positions;
            postData.inCountUnit = data.inCountUnit;
            postData.inCountPrice = data.inCountPrice;
            postData.sumMoneyCount = data.sumMoneyCount;
            postData.colorDescription = data.colorDescription;
            jsonArr.push(postData);
          }
          //开启loading   
          this.loading = true;
          var self = this;

          ajaxRequest(requestUrl, {
            "array": JSON.stringify(jsonArr)
          }).then(function (res) {
            self.$alert(res, '信息');
            self.loading = false;
            self.checkAll = [];
            self.setPurchaseWarehouseInData(true);
          }).catch(function () {
            self.loading = false;
          });
        },
        //选中
        checkOrder: function (selection) {
          this.checkAll = selection;
        },
        isRowShow: function () {
          var data = this.purchaseOrderData[0];
          if (data === undefined) {
            return false;
          }
          return data.type === 1;

        }
      },
      components: {
        warehouseForm: warehouseForm
      }
    });
  </script>
</body>

<style>
  .purchaseWarehouseIn {
    padding: 10px 20px;
    /*    height: calc(100% - 20px); */
    display: flex;
    flex-direction: column;
  }
</style>

</html>